package com.example.baseservice.controller;

import com.example.baseservice.bean.Provider;
import com.example.baseservice.common.LayuiJson;
import com.example.baseservice.common.DataGridView;
import com.example.baseservice.common.ResultObj;
import com.example.baseservice.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("provider")
public class ProviderController {

    @Autowired
    ProviderService providerService;

    @Autowired
    LayuiJson layuiJson;

    /**
     * 根据条件动态查询所有供应商
     *
     * @param
     * @return
     */
    @RequestMapping("loadAllProvider")
    public LayuiJson<Provider> loadAllProvider(String providername, String connectionperson, String phone, Integer page, Integer limit) {
        page = page == null ? 1 : page;
        limit = limit == null ? 5 : limit;
        int index = (page - 1) * limit;
        List<Provider> data = providerService.select(providername, connectionperson, phone, index, limit);
        layuiJson.setCode(0);
        layuiJson.setCount(1000);
        layuiJson.setData(data);
        return layuiJson;
    }

    /**
     * 可选供应商（下拉框）
     *
     * @param
     * @return
     */
    @RequestMapping("loadAllProviderForSelect")
    public DataGridView loadAllProviderForSelect() {
        List<Provider> list = this.providerService.selectAvailable();
        return new DataGridView(list);
    }

    /**
     * 添加一个供应商
     *
     * @param
     * @return
     */
    @RequestMapping("addProvider")
    public ResultObj addProvider(String providername, String zip, String address, String telephone, String connectionperson,
                                 String phone, String bank, String account, String email, String fax, Integer available) {
        try {
            providerService.insert(providername, zip, address, telephone, connectionperson
                    , phone, bank, account, email, fax, available);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }


    /**
     * 修改一个供应商
     *
     * @param
     * @return
     */
    @RequestMapping("updateProvider")
    public ResultObj updateProvider(Integer id, String providername, String zip, String address, String telephone,
                                    String connectionperson, String phone, String bank, String account, String email, String fax,
                                    Integer available) {
        try {
            providerService.update(id, providername, zip, address, telephone, connectionperson, phone, bank, account, email, fax, available);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    /**
     * 删除一个供应商
     *
     * @param id
     * @return
     */
    @RequestMapping("deleteProvider")
    public ResultObj deleteProvider(Integer id) {
        try {
            providerService.delete(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}